/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.service.category;

import com.david.documentation.domain.dao.category.CategoryDao;
import com.david.documentation.domain.entities.category.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(value = "categoryService")
@Transactional(rollbackFor = Exception.class)
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	private CategoryDao categoryDao;

	public Category findById(Long id) {
		return categoryDao.findById(id);
	}

	public void save(Category category) {
		categoryDao.merge(category);
	}

	public Category update(Category category) {
		return categoryDao.merge(category);
	}

	public void remove(Category category) {
		categoryDao.removeSafely(category);
	}

	public long size() {
		return categoryDao.size();
	}

	public List<Category> findAll() {
		return categoryDao.findAll();
	}

	@Deprecated
	public boolean categoryExists(String name) {
		List<String> categories = new ArrayList<String>();
		for (Category tmp : findAll()){
			if (!categories.contains(name)){
				categories.add(tmp.getName());
			}
		}
		return categories.contains(name);
	}

	public Category findCategoryByName(String name) {
		return categoryDao.findByProperty("name",name);
	}

	public boolean checkforDublicate(String name){
		return categoryDao.checkforDublicate(name);
	}

	public Number getNumberOfDokus(Long catId) {
		return categoryDao.getNumberOfDokus(catId);
	}

	public List<Category> findCategorysByName(String name) {
		return categoryDao.findCategorysByName(name);

	}

}
